package book;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Book implements Comparable<Book>{
    private int bookId;
    private String bookName;
    private String author;
    private String category;
    private int publishYear;
    private boolean isBorrowed;
    private int borrowCount;
    private LocalDate shelfDate;

    public int getBookId() {
        return bookId;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getPublishYear() {
        return publishYear;
    }

    public void setPublishYear(int publishYear) {
        this.publishYear = publishYear;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }

    public int getBorrowCount() {
        return borrowCount;
    }

    public void setBorrowCount(int borrowCount) {
        this.borrowCount = borrowCount;
    }

    public LocalDate getShelfDate() {
        return shelfDate;
    }

    public void setShelfDate(LocalDate shelfDate) {
        this.shelfDate = shelfDate;
    }

    public Book(String bookName, String author, String category, int publishYear, LocalDate shelfDate) {
        this.bookName = bookName;
        this.author = author;
        this.category = category;
        this.publishYear = publishYear;
        this.shelfDate = shelfDate;
    }



    void increaseBorrowCount(){
        this.borrowCount++;
    }

    void decreaseBorrowCount(){
        this.borrowCount--;
    }

    public String toJson(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(bookId).append(",");
        stringBuilder.append(bookName).append(",");
        stringBuilder.append(author).append(",");
        stringBuilder.append(category).append(",");
        stringBuilder.append(publishYear).append(",");
        stringBuilder.append(isBorrowed).append(",");
        stringBuilder.append(borrowCount).append(",");
        stringBuilder.append(shelfDate != null ?
                shelfDate.format(DateTimeFormatter.ISO_LOCAL_DATE) : "null");
        return stringBuilder.toString();
    }

    @Override
    public int compareTo(Book o) {
        return o.borrowCount - this.borrowCount;
    }
    @Override
    public String toString() {
        return "Book{" +
                "序号=" + bookId +
                ", 书名='" + bookName + '\'' +
                ", 作者='" + author + '\'' +
                ", 类别='" + category + '\'' +
                ", 出版年份=" + publishYear +
                ", 是否借出：=" + ((isBorrowed)?"已借出":"未借出")+
                ", 借阅次数=" + borrowCount +
                ", 上架时间=" + shelfDate +
                '}';
    }
}
